package com.example.swd1.gudong.main.sport.normalsport;

import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;

import com.example.swd1.gudong.R;

import java.util.List;

//==================================================================================================================
//   Author :  Shile Wu
//   Date  :  16-8-19
//   Role :    xx
//   Reference documents : xx
//==================================================================================================================
public class DoingSportService extends Service {

    private LocationManager locationManager;
    private CallBack callBack;
    private List<String> providerList;
    private Location location;
    private String provider;
    private Location simulateLocation;
    private static boolean canRun = true;//control the simulate data thread
    private Thread thread;
    float lat = 23;//simulate lat
    float lon = 114.35f;//simulate lon

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {

        return new MyBinder();
    }

    public class MyBinder extends Binder {
        public void onPause() {
            canRun = false;
            if(locationManager != null && locationListener != null)
            {
                locationManager.removeUpdates(locationListener);
            }

        }

        public void onRestart() {
            canRun = true;
//            simulateData();
            locationManager.requestLocationUpdates(provider, 5000, 1, locationListener);
        }

        public void setData(Location location) {

        }

        public void setCallBack(CallBack callBack) {
            DoingSportService.this.callBack = callBack;
        }

    }

    public interface CallBack {
        /*
        * dataType:0 is simulate data,1 is real data
        * */
        void onDataChange(Location location,int dataType);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        providerList = locationManager.getProviders(true);
        if (providerList.contains(LocationManager.GPS_PROVIDER)) { //GPS provide
            provider = LocationManager.GPS_PROVIDER;
            Log.d("555log", "service:" + LocationManager.GPS_PROVIDER);
        } else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) { //network provide
            provider = LocationManager.NETWORK_PROVIDER;
            Log.d("555log", "service:" + LocationManager.NETWORK_PROVIDER);
        }
        if (provider != null) {
            if (ActivityCompat.checkSelfPermission(DoingSportService.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(DoingSportService.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            } else {
                locationManager.requestLocationUpdates(provider, 5000, 1, locationListener);
                location = locationManager.getLastKnownLocation(provider);
            }
        }

        canRun = true;
//        simulateData();
    }

    private void simulateData(){
        //************************************************
        ///simulate data
        Toast.makeText(this,"simulate",Toast.LENGTH_SHORT).show();
        simulateLocation = new Location(location);
        thread = new Thread(){
            @Override
            public void run() {
                super.run();
                while (canRun){
                    try {
                        sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        Log.d("555log","thread exception"+e.getMessage());
                    }
                    if (canRun){
                        simulateLocation.setLatitude(lat++);
                        simulateLocation.setLongitude(lon);
                        callBack.onDataChange(simulateLocation,0);
                    }
                }
            }
        };
        thread.start();
        //************************************************
    }

    private final LocationListener locationListener = new LocationListener() {

        public void onLocationChanged(Location location1) {
            callBack.onDataChange(location1,1);
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        locationManager.removeUpdates(locationListener);
    }
}
